#include "rtspmediaplayer.h"
#include <QDebug>

RtspMediaPlayer::RtspMediaPlayer(QObject *parent) : QObject(parent)
{
    mediaPlayer = new QMediaPlayer;
    videoProbe = new QVideoProbe;
    audioProbe = new QAudioProbe;

    connect(videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), this, SLOT(processVideoFrame(QVideoFrame)));
    connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(processAudioBuffer(QAudioBuffer)));
}

void RtspMediaPlayer::setVideoSurface(QAbstractVideoSurface *surface)
{
    mediaPlayer->setVideoOutput(surface);
}
void RtspMediaPlayer::play()
{
    // Supported URI protocols: rtsp/rtspu/rtspt/rtsph/rtsp-sdp
    mediaPlayer->setMedia(QUrl("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov"));
    videoProbe->setSource(mediaPlayer);
    audioProbe->setSource(mediaPlayer);
    mediaPlayer->play();
}
// 下面两个函数可以拿到rtsp流的视频数据和音频数据，可以基于ffmpeg，以软的方式做转码、存文件的动作。
void RtspMediaPlayer::processVideoFrame(const QVideoFrame &videoFrame)
{
    qDebug() << "received video frame:" << videoFrame.pixelFormat();
    qDebug() << "received video frame:" << videoFrame.size();
}

void RtspMediaPlayer::processAudioBuffer(const QAudioBuffer &audioBuffer)
{
    qDebug() << "received audio buffer:" << audioBuffer.byteCount();
}
